package cn.edu.neu.controller.manage;

import cn.edu.neu.model.ScenicSpot;
import cn.edu.neu.service.ScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/scenicSpot")
public class ScenicSpotController {
    @Autowired
    private ScenicSpotService scenicSpotService;

    @RequestMapping("/getScenicSpotList")
    public String getScenicSpotList(Model model){
        List<ScenicSpot> scenicSpotList =scenicSpotService.getScenicSpotList();
        model.addAttribute("scenicspots",scenicSpotList);
        return "/manage/scenicSpot/scenicSpot";
    }
    @RequestMapping("toAddPage")
    public String toAddPage(){
        return "/manage/scenicSpot/addScenicSpot";
    }
    @RequestMapping("/submitScenicSpot")
    public String addScenicSpot(ScenicSpot scenicSpot, MultipartFile fileUpload){
        try {
            File directory = new File("src/main/resources");
            String reportPath = directory.getCanonicalPath();
            String path = "/static/img/travel/";
            String dirPath = reportPath + path;

            File filePath = new File(dirPath);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            String originalFilename = fileUpload.getOriginalFilename();
            fileUpload.transferTo(new File(dirPath + originalFilename));
            scenicSpot.setScenicspot_img("/img/travel/"+originalFilename);

        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

        scenicSpotService.insertScenicSpot(scenicSpot);
        return "redirect:/scenicSpot/getScenicSpotList";
    }

    @RequestMapping("/updateScenicSpot/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        ScenicSpot scenicSpot = scenicSpotService.getScenicSpotById(id) ;
        model.addAttribute("scenicSpot",scenicSpot) ;
        return "/manage/scenicSpot/updateScenicSpot";
    }
    @RequestMapping("/submitUpdateScenicSpot")
    public String updateScenicSpot(ScenicSpot scenicSpot, MultipartFile fileUpload){
        try {
            File directory = new File("src/main/resources");
            String reportPath = directory.getCanonicalPath();
            String path = "/static/img/travel/";
            String dirPath = reportPath + path;

            File filePath = new File(dirPath);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            String originalFilename = fileUpload.getOriginalFilename();
            fileUpload.transferTo(new File(dirPath + originalFilename));
            scenicSpot.setScenicspot_img("/img/travel/"+originalFilename);

        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

        scenicSpotService.updateScenicSpot(scenicSpot) ;
        return  "redirect:/scenicSpot/getScenicSpotList";
    }
    @RequestMapping("/deleteScenicSpot/{id}")
    public String deleteBuy(@PathVariable("id") Integer id){
        int i=scenicSpotService.deleteScenicSpot(id);
        return  "redirect:/scenicSpot/getScenicSpotList";

    }
}
